package com.a.lib_utils;

import android.view.View;
import android.view.ViewGroup;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.appbar.AppBarLayout;

/**
 * 复用的视图控制工具
 */
public class ViewUtil {
    /**
     * 帮助AppBarLayout在CoordinatorLayout时隐藏不留空白
     */
    public static void changeAppbarState(View topView, View view, boolean isVisible) {
        ViewGroup.LayoutParams layoutParams = topView.getLayoutParams();
        CoordinatorLayout.LayoutParams layoutParams1 = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
        if (isVisible) {
            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            layoutParams1.setBehavior(new AppBarLayout.ScrollingViewBehavior());
        } else {
            layoutParams.height = 0;
            layoutParams1.setBehavior(null);
        }
        view.setLayoutParams(layoutParams1);
        topView.setLayoutParams(layoutParams);
        if (isVisible)
            topView.setVisibility(View.VISIBLE);
        else
            topView.setVisibility(View.GONE);
    }

    public static void changeAppbarState(View topView, View view) {
        changeAppbarState(topView, view, topView.getVisibility() != View.VISIBLE);
    }

    /**
     * 判断是否为空，是就隐藏
     */
    public static boolean isNullThenGoneView(View view, String s) {
        if (s == null || s.trim().isEmpty()) {
            view.setVisibility(View.GONE);
            return true;
        } else {
            view.setVisibility(View.VISIBLE);
            return false;
        }
    }
}
